﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Security;

namespace SDF
{
    public class FigureWidthConverter : TypeConverter
    {
        #region Methods

        public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType)
        {
            switch (Type.GetTypeCode(sourceType))
            {
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.String:
                    return true;
            }
            return false;
        }

        public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType)
        {
            if (destinationType != typeof(InstanceDescriptor))
            {
                return (destinationType == typeof(string));
            }
            return true;
        }

        public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
        {
            if (source == null)
            {
                throw base.GetConvertFromException(source);
            }
            if (source is string)
            {
                FigureWidth result;
                if (!FigureWidth.TryParse((string)source, out result))
                    throw new ArgumentException(String.Format("'{0}' cannot be converted into a FigureWidth.", source));

                return result;
            }
            return new FigureWidth(Convert.ToDouble(source, cultureInfo));
        }

        [SecurityCritical]
        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            if ((value != null) && (value is FigureWidth))
            {
                FigureWidth fl = (FigureWidth)value;
                if (destinationType == typeof(string))
                {
                    return fl.ToString();
                }
                if (destinationType == typeof(InstanceDescriptor))
                {
                    return new InstanceDescriptor(
                        typeof(FigureWidth).GetConstructor(new Type[] { typeof(double), typeof(bool) }),
                        new object[] { fl.Value, fl.IsRelativeToParagraph });
                }
            }
            throw base.GetConvertToException(value, destinationType);
        }

        #endregion

    }


}
